from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from order.models import Coupons, UserCoupon, Orders, OrderDetails


class CouponSer(ModelSerializer):
    class Meta:
        model = Coupons
        fields = '__all__'


class UserCouponSer(ModelSerializer):
    coupon = SerializerMethodField(read_only=True)

    def get_coupon(self, obj):
        return {
            'name': obj.coupon.name,
            'starttime': obj.coupon.starttime,
            'endtime': obj.coupon.endtime,
            'price': obj.coupon.price,
            'condition': obj.coupon.condition
        }

    class Meta:
        model = UserCoupon
        fields = '__all__'


class Detail(ModelSerializer):
    class Meta:
        model = OrderDetails
        fields = ['coursename', 'courseid']


class OrdersSer(ModelSerializer):
    orderdetails_set = Detail(read_only=True, many=True)

    class Meta:
        model = Orders
        fields = '__all__'


class OrderDetailsSer(ModelSerializer):
    class Meta:
        model = OrderDetails
        fields = '__all__'
